<?php
namespace app\shop\model;

use app\BaseModel;
use longbingcore\wxcore\Bird;
use think\facade\Db;

class ExpressOrder extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_order_express';


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        if(empty($data)){

            $this->insert($dis);

            $data = $this->where($dis)->find();

        }

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-04-12 16:41
     * @功能说明:快递详情
     */
    public function expressInfo($order_id,$uniacid,$update){

        $express_model = new Express();
        //快递配置
        $express_config= $express_model->dataInfo(['uniacid'=>$uniacid]);

        $key = 'express_order'.$order_id;
        //清除缓存
        if($update==1){

            delCache($key,$uniacid);
        }
        //获取缓存
        $info = getCache($key,$uniacid);

        if(empty($info)){

            $dis = [
                //商城订单
                'type'         => 1,
                //快递鸟
               // 'express_type' => $express_config['type'],

                'order_id'     => $order_id
            ];

            $data = $this->dataInfo($dis);

            if(!empty($data)){

                if($express_config['type']==1){

                    $bird_model = new Bird($uniacid);

                    $info = $bird_model->getOrderTracesByJson($data);

                    $info = json_decode($info,true);

                }
            }
            //存入缓存 两小时有效
            setCache($key,$info,7200,$uniacid);

        }

        return !empty($info)?$info:[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-04-13 09:27
     * @功能说明:添加快递信息
     */
    public function expressAdd($order_id,$uniacid,$send){

        $express_model = new Express();
        //快递配置
        $express_config= $express_model->dataInfo(['uniacid'=>$uniacid]);
        //
        if($express_config['type']==0){

            return true;
        }

        $dis = [
            //商城订单
            'type'         => 1,
            //快递鸟
            //'express_type' => $express_config['type'],

            'order_id'     => $order_id,

            'uniacid'      => $uniacid
        ];

        $this->where($dis)->delete();

        $dis['shipper_code'] = $send['shipper_code'];

        $dis['express_id']   = $send['express_id'];

        $res = $this->insert($dis);

        return $res;

    }







}